package persistence;


import java.lang.reflect.Method;


public class ManyToOneProperty extends PersistentProperty {
	
	
	public ManyToOneProperty(PersistentClass pc, Method m) {
		super(pc, m);
		
		this.setColumnName(this.getColumnName() + "_id");
		this.setSqlType("BIGINT");
	}
	
	
	public Object getValue(Object bean) throws PersistenceException {
		try {
			System.out.println(bean.getClass() + ": get manyToOneProperty " + getName());
			/*
				Category cat = ((Book) bean).getCategory();
				System.out.println("Category: " + cat.getId());
				return cat.getId();
			*/
			Object result = super.getValue(bean);
			
			PersistenceConfiguration pc = PersistenceSessionFactory.currentSession.get().getConnectionConfiguration();
			PersistentClass pcl = pc.getPersistentClass(result.getClass());
			
			Object result2 = pcl.getId().getValue(result);
			
			return result2;
		} catch (Exception e) {
			throw new PersistenceException("Cannot get property " + getName() + " on object of class " + bean.getClass());
		}
	}
	public void setValue(Object bean, Object value) throws PersistenceException {
		try {
			//System.out.println(bean.getClass() + ": set manyToOneProperty " + getName());
			/*
			if(this.getReferentClass().getSimpleName() == "Category") {
				Category cat = new Category();
				cat.setId((Long) value);
				((Book) bean).setCategory(cat);
			}
			*/
		} catch (Exception e) {
			throw new PersistenceException("Cannot set property " + getName() + " on object of class " + bean.getClass());
		}
	}
}
